package com.mz.mattress.admin.service.impl;

import com.mz.mattress.admin.mapper.GoodsMapper;
import com.mz.mattress.admin.service.GoodsService;
import com.mz.mattress.api.common.Result;
import com.mz.mattress.api.entity.Goods;
import com.mz.mattress.api.util.MathUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
@Slf4j
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    private GoodsMapper goodsMapper;

//    @Override
//    public Result<IPage<Goods>> listPage(String name, Integer status, Integer pageNo, Integer pageSize) {
//        Page page = new Page(pageNo, pageSize);
////        Page<Goods> list = goodsMapper.listPage(page, province, goods, status);
////        return Result.ok(list);
//        return Result.ok();
//    }

    @Override
    public Result<List<Goods>> list() {
        List<Goods> list = goodsMapper.list();
        return Result.ok(list);
    }

    @Override
    public Result<Goods> getById(Integer id) {
        Goods goods = goodsMapper.selectById(id);
        return Result.ok(goods);
    }

    @Override
    public Result add(Goods goods) {

        if (goods.getCount() == null) {
            return Result.fail("请输入次数");
        }

        if (goods.getPrice() == null) {
            return Result.fail("请输入价格");
        }

        Integer perMoney = MathUtil.divide(goods.getPrice(), goods.getCount());
        goods.setPerMoney(perMoney);

        goodsMapper.insert(goods);
        return Result.ok();
    }

    @Override
    public Result updateById(Goods goods) {

        if (goods.getCount() == null) {
            return Result.fail("请输入次数");
        }

        if (goods.getPrice() == null) {
            return Result.fail("请输入价格");
        }

        Integer perMoney = MathUtil.divide(goods.getPrice(), goods.getCount());
        goods.setPerMoney(perMoney);

        goodsMapper.updateById(goods);
        return Result.ok();
    }

    @Override
    public Result deleteByIds(Set<Integer> ids) {
        goodsMapper.deleteBatchIds(ids);
        return Result.ok();
    }

}
